package com.jinlejia.erp.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityCancelReasonBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.weight.CustomToast;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 取消原因（Activity）.
 */

public class CancelReasonActivity extends AppCompatActivity {

    private JSONObject jsonObject = new JSONObject();
    private InputMethodManager imm;

    private ActivityCancelReasonBinding binding;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_cancel_reason);
        binding.btBack.setOnClickListener(this::onClick);
        binding.btSave.setOnClickListener(this::onClick);
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        initView();
    }

    private void initView() {
        binding.inputContent.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                temp = s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                int number = 200 - s.length();
                binding.textCount.setText(number + "/200");
                selectionStart = binding.inputContent.getSelectionStart();
                selectionEnd = binding.inputContent.getSelectionEnd();
                if (temp.length() > 200) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    binding.inputContent.setText(s);
                    binding.inputContent.setSelection(tempSelection);//设置光标在最后
                }
            }
        });
    }



    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_save:
                jsonObject.put("cancleReason",binding.inputContent.getText().toString());
                if (binding.inputContent.length() > 0 && binding.inputContent.length() <= 200 ){
                    getData();
                }else {
                    CustomToast.showToast(this,"请输入原因至少200字以内",2000);
                }
                // 隐藏软键盘
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                break;
        }
    }

    /**
     * 获取数据
     */
    private void getData(){

        jsonObject.put("appointmentCode",getIntent().getStringExtra("appointmentCode"));
        OkHttpUtils
                .postString()
                .url(ApiConfig.CANCLE_APPOINTMENT)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(CancelReasonActivity.this, response,null)) {
                            CustomToast.showToast(CancelReasonActivity.this,"提交成功",2000);
                            finish();
                        }
                    }
                });
    }

}
